//
//  LSLabelLayer.m
//  WCGradientLayerExample
//
//  Created by 陈晓强 on 2016/12/20.
//  Copyright © 2016年 huangwenchen. All rights reserved.
//

#import "LSTextLayer.h"
#import <UIKit/UIKit.h>
#import "UIView+Frame.h"
#import <CoreText/CoreText.h>

static NSString *kAnimationKey = @"text";

@interface LSTextLayer() <CAAnimationDelegate>

@end

@implementation LSTextLayer
@dynamic textString;
@synthesize animateValue;
@synthesize currentValue;

- (NSString *)animateKeypath {
    return @"textString";
}

+ (BOOL)needsDisplayForKey:(NSString*)key
{
    if([key isEqualToString:@"textString"]) {
        return YES;
    }
    return[super needsDisplayForKey:key];
} 

//- (void)display {
//    [super display];
//    self.string = [self.presentationLayer textString];
//}

- (void)drawInContext:(CGContextRef)context {
    CGFloat fontSize = 20; 
    CGFloat textLength = [self.textString boundingRectWithSize:CGSizeMake(1000, self.bounds.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size.width;
    CGFloat x = self.bounds.size.width / 2.0 - textLength / 2.0;
    CGFloat y = self.bounds.size.height / 2.0 - fontSize / 2.0;
    UIGraphicsPushContext(context); 
    {
    [self.textString drawAtPoint:CGPointMake(x, y) withAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont systemFontOfSize:fontSize]}];
    } 
    UIGraphicsPopContext();
}

- (id<CAAction>)actionForKey:(NSString *)key
{
    if ([key isEqualToString:@"textString"])
        {
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:key];
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        animation.fromValue = [self.presentationLayer textString];
//        animation
        return animation;
        }
    return [super actionForKey:key];
}

- (instancetype)initWithFrame:(CGRect)rect text:(NSString *)text {
    if (self = [super init]) {
        self.frame = rect;
        self.textString = text;
//        self.string = text;
        self.font = CFBridgingRetain([UIFont systemFontOfSize:14]);
        self.foregroundColor = [UIColor clearColor].CGColor;
        self.currentValue = text;
        self.animateValue = text;
    }
    return self;
}

- (void)addTextAnimation {
    CAKeyframeAnimation *ani = [CAKeyframeAnimation animationWithKeyPath:@"string"];
    ani.values = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20"];
    ani.duration = 1.8;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    ani.fillMode = kCAFillModeForwards;
    ani.removedOnCompletion = NO;
    ani.beginTime = 0;
    ani.delegate = self;
    [self addAnimation:ani forKey:kAnimationKey];
    
//    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"bounds"];
////    anim.duration = [self per_duration];
//    anim.fromValue = [NSValue valueWithCGRect: self.bounds];
//    anim.toValue = [NSValue valueWithCGRect:CGRectZero];
//    anim.fillMode = kCAFillModeForwards;  
//    anim.removedOnCompletion = NO;  
//    //    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//    //    anim.delegate = self;  
////    anim.fromValue = @"10";
//////    anim.beginTime = CACurrentMediaTime() + 2;
////    anim.toValue = @"20";
////    animation.fromValue =  [NSValue valueWithCGPoint: self.position];
//    anim.delegate = self;
//    self.string = @"dsdf";
//    [self addAnimation:anim forKey:@"eee"];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
//    CAKeyframeAnimation *ani = (CAKeyframeAnimation *)anim;
//    self.string = [ani.values lastObject];
//    [self removeAnimationForKey:kAnimationKey];
}
@end
